package com.ossjk.qlh.specializedSubject.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.specializedSubject.dto.Subjectdto;
import com.ossjk.qlh.specializedSubject.entity.Subject;
import com.ossjk.qlh.specializedSubject.service.ISubjectService;
import com.ossjk.qlh.system.entity.Department;
import com.ossjk.qlh.system.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * Copyright  2023-03-06 QLH. Tech Ltd. All rights reserved.
 * 
 * @Package: com.ossjk.qlh.specializedSubject.controller
 * @ClassName: SubjectController
 * @Description: Subject-控制器
 * @author: Levi
 * @date:  2023-03-06 15:46:51 
 */
@Api(tags = "专业表")
@RestController
@RequestMapping("/specializedSubject/subject")
public class SubjectController extends BaseController {
	
	@Autowired
	private ISubjectService iSubjectService;

	@ApiOperation(value = "列表")
	@RequiresPermissions("00050010")
	@GetMapping("/list")
	public ResponseBean<Page<Subject>> list(Page page,String name) {

		QueryWrapper<Subject> queryWrapper = new QueryWrapper<Subject>();
		if (name!=null){
			queryWrapper.like("name",name);
		}

		return ResponseBean.Success(iSubjectService.page(page,queryWrapper));
	}

	@ApiOperation(value = "添加")
	@RequiresPermissions("000500100010")
	@PostMapping("/save")
	public ResponseBean save(@RequestBody Subject record) {
		if (iSubjectService.save(record)) {
			return ResponseBean.Success();
		} else {
			return ResponseBean.Fail();
		}
	}

	@ApiOperation(value = "去编辑")
	@RequiresPermissions("000500100005")
	@GetMapping("/toUpdate")
	public ResponseBean<Subjectdto> toUpdate(@ApiParam(value = "id") @RequestParam(name = "id") String id) {
		Subject subject = iSubjectService.getById(id);
		if (ObjectUtil.isNotNull(subject)) {

			List<Department> list =	iSubjectService.selectbypianma();

			Subjectdto subjectdto=new Subjectdto();
			subjectdto.setSubject(subject);
			subjectdto.setDepts(list);
			return ResponseBean.Success(subjectdto);
		} else {
			return ResponseBean.Fail();
		}
	}
//	@RequiresPermissions("000500100005")
//	@GetMapping("/getlist")
//	public ResponseBean<List<Department>> toUpdatelist() {
//
//
//			List<Department> list =	iSubjectService.selectbypianma();
//
//
//			return ResponseBean.Success(list);
//
//	}

	@ApiOperation(value = "编辑")
	@RequiresPermissions("000500100010")
	@PutMapping("/update")
	public ResponseBean update(@RequestBody Subject record) {

		if (iSubjectService.updateById(record)) {
			return ResponseBean.Success();
		} else {
			return ResponseBean.Fail();
		}
	}

	@ApiOperation(value = "删除")
	@RequiresPermissions("000500100015")
	@DeleteMapping("/remove")
	public ResponseBean remove(@ApiParam(value = "ids") @RequestParam(name = "ids") String[] ids) {
		if (iSubjectService.removeByIds(Arrays.asList(ids))) {
			return ResponseBean.Success();
		} else {
			return ResponseBean.Fail();
		}
	}
}
